package wiimote.WiimoteJ;

import java.awt.Point;
import java.io.IOException;

import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.JoystickEvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.physicalevents.NunchukEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

public class NunchukWiimoteJ implements WiimoteListener{
	//-Dbluecove.stack=widcomm

	static OmniController omni;
	private static String nxtName = "LejosNxt";
	
	public void onButtonsEvent(WiimoteButtonsEvent arg0) {
		//		System.out.println(arg0);
		if (arg0.isButtonAPressed()){
			WiiUseApiManager.shutdown();
		}
	}

	public void onIrEvent(IREvent arg0) {
		//		System.out.println(arg0);
	}

	public void onMotionSensingEvent(MotionSensingEvent arg0) {
		//		System.out.println(arg0);
	}

	public void onExpansionEvent(ExpansionEvent evt) {
		if (evt instanceof NunchukEvent) {
			NunchukEvent nunchuk = (NunchukEvent) evt;

			JoystickEvent nunchukJoystickEvent = nunchuk.getNunchukJoystickEvent();
			if(nunchukJoystickEvent != null){
				float angle = (360-nunchukJoystickEvent.getAngle()+90)%360;
				float magnitude = nunchukJoystickEvent.getMagnitude();
				int power = 100;
				
				int x = (int) Math.max(Math.cos(Math.toRadians(angle))*magnitude*power, -power);
				int y = (int) (Math.sin(Math.toRadians(angle))*magnitude + 0.07)*power;
				
				omni.analogStickChanged(new Point(x, y), false);
				
//				System.out.println("angle: " + angle + "    \tmag: " + magnitude);
				System.out.println(x + " " + y);
//				System.out.println(evt);
			}
		}
	}

	public void onStatusEvent(StatusEvent arg0) {
		//        System.out.println(arg0);
	}

	public void onDisconnectionEvent(DisconnectionEvent arg0) {
		//		System.out.println(arg0);
	}

	public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
		//		System.out.println(arg0);
	}

	public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
		//		System.out.println(arg0);
	}

	public static void main(String[] args) throws IOException {
		omni = new OmniController();
		omni.openConnection(nxtName);
		omni.analogStickChanged(new Point(100,100), false);
		
		
		Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true, WiiUseApiManager.WIIUSE_STACK_MS); 
		Wiimote wiimote = wiimotes[0];
		wiimote.activateIRTRacking();
		wiimote.activateMotionSensing();
		wiimote.addWiiMoteEventListeners(new NunchukWiimoteJ());
	}

	@Override
	public void onClassicControllerInsertedEvent(
			ClassicControllerInsertedEvent e) {
	}

	@Override
	public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent e) {	}
	@Override
	public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent e) {}
	@Override
	public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent e) {}
}
